****************************************************************************************************************************
Date: 04 FEB 2016

Purpose: Add indicator for stillbirth
		
****************************************************************************************************************************;

libname in 'G:\Data\Rawdata\702727';

/************************************************************
*Indhenter information om dødsmarkering.
Dødsmarkeringen angiver om barnet er:
1:dødfødt
2:død 1. uge 
3:død 2.-4. uge
4:død 1.-11. måned
5.overlever første år
**************************************************************/
data kvindeborn;
set in.kvindeboern_08(rename=(pnr=pnrm));
label mdoed="død-markering";

year=year(FODTDATO);
if kqn=1 then gender=1;
if kqn=2 then gender=0;
run;

data stillborns;
set kvindeborn (drop=kqn);
stillborn_fam=1;
if mdoed=1 then output stillborns;
run;

/*==================================================*

TJEKKER MED DANSK STUDIE FRA BMJ
	DE FINDER 7021 STILLBORNS I PERIODEN 1973-1996 

*===================================================*/
data test;
set stillborns;
if year>1996 then delete;
run;
proc freq data=test;
table year;
run;		/*HER FINDER JEG 7669 STILLBORNS I SAMME PERIODE*/


data stillborns1;
set stillborns (drop=mdoed rename=(gender=still_gender) rename=(FODTDATO=still_date));

label still_gender="Gender of stillborn"; 
label still_date="Date of stillbirth"; 
run;

proc sort data=stillborns1;
by pnrm still_date;
run;

data stillborns1;
set stillborns1;
by pnrm;
count+1;
if first.pnrm then count=1;
run;

proc freq data=stillborns1;
table count;
run;

data stillborns1;
set stillborns1;

stillborn1_fam=.;
if count=1 then do;
stillborn1_fam=1;
temp_still1_gender=still_gender;
still1_date=still_date;
end;

still1_gender=temp_still1_gender*1;

stillborn2_fam=.;
if count=2 then do;
stillborn2_fam=1;
temp_still2_gender=still_gender;
still2_date=still_date;
end;

still2_gender=temp_still2_gender*1;

stillborn3_fam=.;
if count=3 then do;
stillborn3_fam=1;
temp_still3_gender=still_gender;
still3_date=still_date;
end;

still3_gender=temp_still3_gender*1;

run;

data stillborns2;
set stillborns1 (drop= count stillborn_fam still_gender still_date temp_still1_gender temp_still2_gender temp_still3_gender);
run;

proc means data=stillborns2 NWAY NOPRINT;
class pnrm;
Output Out=stillborns3 min=;
run;

data stillborns3;
set stillborns3 (drop= _FREQ_ _TYPE_);
run;



*STATA datasæt;
proc export data=stillborns3 outfile="G:\Data\Workdata\702727\xru\NICU project\Datasæt\stillborns.dta" dbms=stata replace;
run;







******************************************************
*Henter NICU data som dette skal kobles sammen med
******************************************************;
libname in 'G:\Data\Workdata\702727\xru\NICU project\Datasæt';

data nicu;
set in.nicu_parentalhealth (keep = pnr pnrm pnrf FODTDATO birthyear male); 
run;

proc freq data=nicu;
table birthyear;
run;

proc sort data=nicu;
by pnrm fodtdato;
run;

*Merger dødsmarkering ud fra mors pnr og fødselsdato;
data nicu_stillborn;
merge nicu (in=a) stillborns3;
by pnrm;
if a;
run;

/*************************************************
- SÆT TIL NUL HVIS 0
- SÆT TIL MISSING HVIS UDENFOR PERIODE
***************************************************/

data nicu_stillborn1;
set nicu_stillborn;
if stillborn1_fam=. then stillborn1_fam=0;
if stillborn2_fam=. then stillborn2_fam=0;
if stillborn3_fam=. then stillborn3_fam=0;

if birthyear<1973 then do; 
stillborn1_fam=.;
stillborn2_fam=.;
stillborn3_fam=.;
end;

if birthyear>2008 then do;
stillborn1_fam=.;
stillborn2_fam=.;
stillborn3_fam=.;
end;
run;



*************
Gemmer data
*************;

*SAS datasæt;
libname out 'G:\Data\Workdata\702727\xru\NICU project\Datasæt';

data out.nicu_stillborn;
set nicu_stillborn1;
run;

*STATA datasæt;
proc export data=nicu_stillborn1 outfile="G:\Data\Workdata\702727\xru\NICU project\Datasæt\nicu_stillborn.dta" dbms=stata replace;
run;







*****************************************
Gemmer data til OLS stillborn analyse
*****************************************;

libname in 'G:\Data\Workdata\702727\xru\NICU project\Datasæt';

data nicu1;
set in.nicu_parentalhealth; 
run;

proc sort data=nicu1;
by pnrm;
run;


data stillborn_analysis;
merge nicu1 (in=a) nicu_stillborn1;
by pnrm;
if a;
run;



*******************
Vælger min sample
*******************;
*Dropper de faktiske stillborns (eller deres tvilling...);
data stillborn_analysis1;
set stillborn_analysis;

if still1_date=fodtdato then delete;
if still2_date=fodtdato then delete;
if still3_date=fodtdato then delete;
run;

*Danner nu indikator for om stillborn er før eller efter focal child fødsel;

data stillborn_analysis1;
set stillborn_analysis1;
still_before=0;
if still1_date<fodtdato and still1_date^=. then still_before=1;
still_after=0;
if still1_date>fodtdato and still1_date^=. then still_after=1;

if stillborn1_fam=. then still_before=.;
if stillborn1_fam=. then still_after=.;

run;

proc freq data=stillborn_analysis1;
table stillborn1_fam still_before still_after;
run;


*Selecting sample;
*By cohort;
*By outcomes - Math and Reading.;
*Saving the sample for later analysis;
data stillborn_analysis2;
set stillborn_analysis1;
if birthyear<1986 then delete;
if birthyear>1997 then delete;

if Dansk_m_prove1=. then delete;
if Mat_s_prove1=. then delete;

run;

proc freq data=stillborn_analysis2;
table family_size*stillborn1_fam; 
run;


*Gemmer nu Sample til analyse;
*STATA datasæt;
proc export data=stillborn_analysis2 outfile="G:\Data\Workdata\702727\xru\NICU project\Datasæt\stillborn_analysis.dta" dbms=stata replace;
run;










